<template>
  <el-form ref="form" :model="form" label-width="80px">
    <el-form-item label="商品名称">
      <el-input v-model="form.Spuname" placeholder="商品名称"></el-input>
    </el-form-item>
    <!-- <el-form-item label="商品类型">
      <el-cascader
        v-model="value"
        :options="options"
        @change="handleChange"
        style="width: 500px"
        placeholder="请选择商品的类型"
        @click="handleChangeaa()"
      ></el-cascader>
    </el-form-item> -->
    <!-- <el-form-item label="商品类型">
      <el-select v-model="form.region" placeholder="请选择活动区域">
        <div v-for="item in this.subclassifyandClassify" :key="item.id">
          <el-option
            :label="item.classifyname"
            :value="item.classifyname"
            @change="shangpinleixing(item.id)"
          ></el-option>
        </div>
      </el-select>
    </el-form-item> -->
    <el-form-item label="商品类型">
      <div>
        <el-radio-group
          v-model="form.resourcesp"
          v-for="item in this.subclassifyandClassify"
          :key="item.id"
        >
          <el-radio
            :label="item.id"
            :values="item.id"
            @change="shangpinleixing(item.id)"
            >{{ item.classifyname }}</el-radio
          >
        </el-radio-group>
      </div>
    </el-form-item>
    <el-form-item label="商品分类">
      <div>
        <el-radio-group
          v-model="form.resource"
          v-for="item in this.subclassifyandClassifyss"
          :key="item.id"
        >
          <el-radio
            :label="item.subclassifyfyname"
            @change="shangpinfenlei(item.id)"
            >{{ item.subclassifyfyname }}</el-radio
          >
        </el-radio-group>
      </div>
    </el-form-item>
    <div v-for="item in this.subclassifyandClassifyssaa" :key="item.id">
      <!-- <el-form-item :label="item.specificationname">
        <el-radio-group
          v-model="checkedCities"
          @change="handleCheckedCitiesChange"
        >
          <div v-for="itemss in subclassifyandClassifyssaarnm" :key="itemss.id">
            <el-checkbox
              v-if="item.id == itemss.specificationId"
              :label="itemss.specificationvaluename"
              @change="
                shangpinapp(
                  item.specificationname,
                  itemss.specificationvaluename
                )
              "
            ></el-checkbox>
          </div>
        </el-radio-group>
      </el-form-item> -->
      <el-form-item
        :label="item.specificationname"
        :value="item.id"
        v-model="checkedCitiess"
      >
        <el-select
          v-model="checkedCities"
          multiple
          placeholder="请从图库列表中选择商品需要的图片id"
          style="width: 500px"
          :value="item.id"
          @change="shangpinapp(item.id)"
        >
          <div v-for="itemss in subclassifyandClassifyssaarnm" :key="itemss.id">
            <el-option
              v-if="item.id == itemss.specificationId"
              :key="item.id"
              :label="itemss.specificationvaluename"
              :value="itemss.specificationvaluename"
            >
            </el-option>
          </div>
        </el-select>
      </el-form-item>
    </div>
    <el-form-item>
      <el-button type="primary" plain @click="dialogFormVisible = true"
        >添加规格</el-button
      >
    </el-form-item>

    <el-form-item label="商品价格">
      <el-input v-model="form.Price" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="商品品牌">
      <el-input v-model="form.Brand" placeholder="商品品牌"></el-input>
    </el-form-item>
    <el-form-item label="商品库存">
      <el-input-number
        v-model="num"
        @change="handleChange"
        :min="1"
        :max="10000"
        label="描述文字"
      ></el-input-number>
    </el-form-item>
    <el-form-item label="封面图片">
      <!-- <el-select
        v-model="value2"
        multiple
        placeholder="请从图库列表中选择商品需要的图片id"
        style="width: 500px"
        @change="Change2"
      >
      <el-option
          v-for="item in fileList"
          :key="item.id"
          :label="item.id"
          :value="item.id"
        >
         </el-option>
       </el-select> -->
      <el-select v-model="value2" @change="Change2" placeholder="请选择">
        <el-option
          v-for="item in fileList"
          :key="item.id"
          :label="item.id"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="商品图片">
      <el-select
        v-model="value1"
        multiple
        placeholder="请从图库列表中选择商品需要的图片id"
        style="width: 500px"
        @change="Change"
      >
        <el-option
          v-for="item in fileList"
          :key="item.id"
          :label="item.id"
          :value="item.id"
        >
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item label="图库列表">
      <ul class="infinite-list" style="overflow: auto">
        <el-upload
          class="upload-demo"
          :action="uploadActionUrl"
          :on-preview="handlePreview"
          :file-list="fileList"
          list-type="picture"
        ></el-upload>
      </ul>
    </el-form-item>
    <el-form-item label="商品简介">
      <div style="margin: 5px 0"></div>
      <el-input
        type="textarea"
        placeholder="请输入内容"
        v-model="form.Intro"
        maxlength="30"
        show-word-limit
      >
      </el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">立即创建</el-button>
      <el-button native-type="reset">重置</el-button>
    </el-form-item>
    <el-dialog title="添加规格" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="商品类型" :label-width="formLabelWidth">
          <el-input v-model="form.Classifyname" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="form">
        <el-form-item label="商品分类" :label-width="formLabelWidth">
          <el-input
            v-model="form.Subclassifyfyname"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="form">
        <el-form-item label="商品规格" :label-width="formLabelWidth">
          <el-input
            v-model="form.Specificationname"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="form">
        <el-form-item label="商品规格值" :label-width="formLabelWidth">
          <el-input
            v-model="form.Specificationvaluename"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="tianjiashangpinguige"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </el-form>
</template>
<script>
import { GetImage } from "../api/files";
import {
  getsubclassifyandClassify,
  getid,
  getggandggz,
  postclassify,
  postsubclassify,
  postspecification,
  postspecificationvalue,
  postspu,
  postspuspecification,
  postspuspecificationvalue,
  postspuimage,
  getname,
} from "../api/home";
export default {
  data() {
    return {
      form: {
        Spuname: "",
        Price: "",
        resource: "",
        resources: "",
        resourcegg: "",
        resourcesp: "",
        Classifyname: "",
        ClassifyId: 0,
        Subclassifyfyname: "",
        SubclassifyId: 0,
        Specificationname: "",
        SpecificationId: 0,
        Specificationvaluename: "",
        specificationnamess: "",
        specificationvaluenamess: "",
        Spuspecificationname: "",
        SpuspecificationId: 0,
        Spuspecificationvaluename:"",
        Intro: "",
        Brand: "",
        SpuId: "",
        ImageId: "",
        Num:"",
      },
      froms: {
        ImageId: "",
        SpuId: "",
      },
      values: "",
      input: "",
      value: [],
      //图片数组
      fileList: [],
      drawer: false,
      optionss: [],

      dialogTableVisible: false,
      dialogFormVisible: false,
      value1: [],
      value2: "",
      subclassifyandClassify: [],
      subclassifyandClassifyss: [],
      subclassifyandClassifyssaa: [],
      subclassifyandClassifyssaarnm: [],
      formLabelWidth: "120px",
      num: 1,
      Specificationnameguige: "",
      Specificationnameguigezhi: "",
      checkedCities: [],
      checkedCitiess: [],
      specificationnameaa: [],
    };
  },
  methods: {
    shangpinleixing(id) {
      (this.subclassifyandClassifyssaa = []),
        (this.subclassifyandClassifyssaarnm = []),
        console.log(id);
      getid(id).then((data) => {
        console.log(data);
        this.subclassifyandClassifyss = data.data;
        console.log(this.subclassifyandClassifyss);
        console.log(this.subclassifyandClassifyssaarnm);
      });
    },
    shangpinfenlei(id) {
      console.log(id);
      this.form.SubclassifyId= id
      getggandggz(id).then((data) => {
        console.log(data.data);
        this.subclassifyandClassifyssaa = data.data.data;
        this.subclassifyandClassifyssaarnm = data.data.pager;
        console.log(this.subclassifyandClassifyssaa);
        console.log(this.subclassifyandClassifyssaarnm);
      });
    },
    // shangpinapp(specificationname, specificationvaluename) {
    //   this.Specificationnameguige = specificationname;
    //   console.log(this.Specificationnameguige);
    //   this.form.Spuspecificationname = this.Specificationnameguige;
    //   this.Specificationnameguigezhi = specificationvaluename;
    //   this.form.Spuspecificationvaluename = this.Specificationnameguigezhi;
    //   console.log(this.form.Spuspecificationvaluename);
    // },
    shangpinapp() {
      console.log(this.checkedCities);
    },
    async onSubmit() {
      await postspu(this.form).then((data) => {
        console.log(data.data.id);
        this.form.SpuId = data.data.id;
        console.log(this.form.SpuId);
        this.froms.SpuId = data.data.id;
      });
      // await postspuspecification(this.form).then((data) => {
      //   console.log(data.data);
      //   this.form.SpuspecificationId = data.data.id;
      // });
      // await postspuspecificationvalue(this.form).then((data) => {
      //   console.log(data.data);
      // });
      await this.value1.forEach((element) => {
        console.log(element);
        this.froms.ImageId = element;
        postspuimage(this.froms).then((data) => {
          console.log(data);
        });
      });
      await this.checkedCities.forEach((element) => {
        console.log(element);
        getname(element).then(({ data }) => {
          console.log(data);
          var sss = data;
          sss.forEach((index) => {
            // let obj = [
            //   {
            //     specificationId:index.specificationId,
            //     specificationname:index.specificationname
            //   }
            // ];
            // this.specificationnameaa.push(obj)
            this.add(index);
          });
        });
      });
      console.log(this.specificationnameaa);
    },
    async add(index) {
       this.form.Spuspecificationname = index.specificationname;
      await postspuspecification(this.form).then((data) => {
        console.log(data.data);
        this.form.SpuspecificationId = data.data.id;
      });
       this.form.Spuspecificationvaluename = index.specificationvaluename;
      await postspuspecificationvalue(this.form).then((data) => {
        console.log(data);
      });
    },
    // app(index) {},
    handleChangeaa() {},
    handleChange(label) {
      console.log(label);
      this.form.Num = this.num
      console.log(this.form.Num);
    },
    Change() {
      console.log(this.value1);
    },
    Change2() {
      console.log(this.value2);
      this.form.ImageId = this.value2;
    },
    handlePreview(file) {
      console.log(file);
      console.log(file.indexOf);
      // this.fileList.splice(file.indexOf,1)
    },
    async tianjiashangpinguige() {
      this.dialogFormVisible = false;
      console.log(
        this.form.Classifyname,
        this.form.Subclassifyfyname,
        this.form.Specificationname,
        this.form.Specificationvaluename
      );
      await postclassify(this.form).then((data) => {
        console.log(data);
        console.log(data.code);
        if (data.code == 1000) {
          this.form.ClassifyId = data.data.id;
          console.log(this.form);
        } else if (data.code == 1004) {
          data.data.forEach((element) => {
            this.form.ClassifyId = element.id;
            console.log(this.form);
          });
        }
      });
      await postsubclassify(this.form).then((data) => {
        if (data.code == 1000) {
          this.form.SubclassifyId = data.data.id;
          console.log(this.form);
        } else if (data.code == 1004) {
          data.data.forEach((element) => {
            this.form.SubclassifyId = element.id;
            console.log(this.form);
          });
        }
      });
      await postspecification(this.form).then((data) => {
        if (data.code == 1000) {
          this.form.SpecificationId = data.data.id;
          console.log(this.form);
        } else if (data.code == 1004) {
          data.data.forEach((element) => {
            this.form.SpecificationId = element.id;
            console.log(this.form);
          });
        }
      });
      postspecificationvalue(this.form).then((data) => {
        console.log(data.data);
      });
    },
    fetchData() {
      GetImage()
        .then((data) => {
          var res = data.data;
          console.log(res);
          for (var index = 0; index < res.length; index++) {
            var obj = {
              indexOf: index,
              id: res[index].id,
              name: res[index].id,
              url: "http://localhost:5000/files/" + res[index].id,
            };

            this.fileList.push(obj);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  mounted() {
    getsubclassifyandClassify().then((data) => {
      console.log(data.data.data);
      var res = data.data.data;
      console.log(res);
      res.forEach((element) => {
        this.subclassifyandClassify.push(element);
      });
    });
    this.fetchData();
    console.log(this.fileList);
  },

  computed: {
    uploadActionUrl() {
      const baseUrl = "http://localhost:5000/";
      const uploadUrl = "files/uploadfiles";
      return baseUrl + uploadUrl;
    },
  },
};
</script>
<style scoped>
.upload-demo {
  max-height: 300px;
}
</style>